/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Ron
 */
public class FCalendrierJpaController implements Serializable {

    public FCalendrierJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public FCalendrierJpaController() {
        this.emf = Persistence.createEntityManagerFactory("projetT_RPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FCalendrier FCalendrier) throws PreexistingEntityException, Exception {
        if (FCalendrier.getFRessourceprodCollection() == null) {
            FCalendrier.setFRessourceprodCollection(new ArrayList<FRessourceprod>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<FRessourceprod> attachedFRessourceprodCollection = new ArrayList<FRessourceprod>();
            for (FRessourceprod FRessourceprodCollectionFRessourceprodToAttach : FCalendrier.getFRessourceprodCollection()) {
                FRessourceprodCollectionFRessourceprodToAttach = em.getReference(FRessourceprodCollectionFRessourceprodToAttach.getClass(), FRessourceprodCollectionFRessourceprodToAttach.getCbMarq());
                attachedFRessourceprodCollection.add(FRessourceprodCollectionFRessourceprodToAttach);
            }
            FCalendrier.setFRessourceprodCollection(attachedFRessourceprodCollection);
            em.persist(FCalendrier);
            for (FRessourceprod FRessourceprodCollectionFRessourceprod : FCalendrier.getFRessourceprodCollection()) {
                FCalendrier oldCALNoOfFRessourceprodCollectionFRessourceprod = FRessourceprodCollectionFRessourceprod.getCALNo();
                FRessourceprodCollectionFRessourceprod.setCALNo(FCalendrier);
                FRessourceprodCollectionFRessourceprod = em.merge(FRessourceprodCollectionFRessourceprod);
                if (oldCALNoOfFRessourceprodCollectionFRessourceprod != null) {
                    oldCALNoOfFRessourceprodCollectionFRessourceprod.getFRessourceprodCollection().remove(FRessourceprodCollectionFRessourceprod);
                    oldCALNoOfFRessourceprodCollectionFRessourceprod = em.merge(oldCALNoOfFRessourceprodCollectionFRessourceprod);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFCalendrier(FCalendrier.getCbMarq()) != null) {
                throw new PreexistingEntityException("FCalendrier " + FCalendrier + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FCalendrier FCalendrier) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FCalendrier persistentFCalendrier = em.find(FCalendrier.class, FCalendrier.getCbMarq());
            Collection<FRessourceprod> FRessourceprodCollectionOld = persistentFCalendrier.getFRessourceprodCollection();
            Collection<FRessourceprod> FRessourceprodCollectionNew = FCalendrier.getFRessourceprodCollection();
            Collection<FRessourceprod> attachedFRessourceprodCollectionNew = new ArrayList<FRessourceprod>();
            for (FRessourceprod FRessourceprodCollectionNewFRessourceprodToAttach : FRessourceprodCollectionNew) {
                FRessourceprodCollectionNewFRessourceprodToAttach = em.getReference(FRessourceprodCollectionNewFRessourceprodToAttach.getClass(), FRessourceprodCollectionNewFRessourceprodToAttach.getCbMarq());
                attachedFRessourceprodCollectionNew.add(FRessourceprodCollectionNewFRessourceprodToAttach);
            }
            FRessourceprodCollectionNew = attachedFRessourceprodCollectionNew;
            FCalendrier.setFRessourceprodCollection(FRessourceprodCollectionNew);
            FCalendrier = em.merge(FCalendrier);
            for (FRessourceprod FRessourceprodCollectionOldFRessourceprod : FRessourceprodCollectionOld) {
                if (!FRessourceprodCollectionNew.contains(FRessourceprodCollectionOldFRessourceprod)) {
                    FRessourceprodCollectionOldFRessourceprod.setCALNo(null);
                    FRessourceprodCollectionOldFRessourceprod = em.merge(FRessourceprodCollectionOldFRessourceprod);
                }
            }
            for (FRessourceprod FRessourceprodCollectionNewFRessourceprod : FRessourceprodCollectionNew) {
                if (!FRessourceprodCollectionOld.contains(FRessourceprodCollectionNewFRessourceprod)) {
                    FCalendrier oldCALNoOfFRessourceprodCollectionNewFRessourceprod = FRessourceprodCollectionNewFRessourceprod.getCALNo();
                    FRessourceprodCollectionNewFRessourceprod.setCALNo(FCalendrier);
                    FRessourceprodCollectionNewFRessourceprod = em.merge(FRessourceprodCollectionNewFRessourceprod);
                    if (oldCALNoOfFRessourceprodCollectionNewFRessourceprod != null && !oldCALNoOfFRessourceprodCollectionNewFRessourceprod.equals(FCalendrier)) {
                        oldCALNoOfFRessourceprodCollectionNewFRessourceprod.getFRessourceprodCollection().remove(FRessourceprodCollectionNewFRessourceprod);
                        oldCALNoOfFRessourceprodCollectionNewFRessourceprod = em.merge(oldCALNoOfFRessourceprodCollectionNewFRessourceprod);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = FCalendrier.getCbMarq();
                if (findFCalendrier(id) == null) {
                    throw new NonexistentEntityException("The fCalendrier with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FCalendrier FCalendrier;
            try {
                FCalendrier = em.getReference(FCalendrier.class, id);
                FCalendrier.getCbMarq();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The FCalendrier with id " + id + " no longer exists.", enfe);
            }
            Collection<FRessourceprod> FRessourceprodCollection = FCalendrier.getFRessourceprodCollection();
            for (FRessourceprod FRessourceprodCollectionFRessourceprod : FRessourceprodCollection) {
                FRessourceprodCollectionFRessourceprod.setCALNo(null);
                FRessourceprodCollectionFRessourceprod = em.merge(FRessourceprodCollectionFRessourceprod);
            }
            em.remove(FCalendrier);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FCalendrier> findFCalendrierEntities() {
        return findFCalendrierEntities(true, -1, -1);
    }

    public List<FCalendrier> findFCalendrierEntities(int maxResults, int firstResult) {
        return findFCalendrierEntities(false, maxResults, firstResult);
    }

    private List<FCalendrier> findFCalendrierEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FCalendrier.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FCalendrier findFCalendrier(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FCalendrier.class, id);
        } finally {
            em.close();
        }
    }

    public int getFCalendrierCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FCalendrier> rt = cq.from(FCalendrier.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
